package com.rebate.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.rebate.model.Users;
import com.rebate.service.IBaseService;
import com.rebate.util.MD5Util;


@Controller
public class UserServlet {
	
	@Autowired
	private IBaseService<Users> userService;
	
	/**
	 * 后台用户登录
	 * @throws IOException 
	 */
	@RequestMapping(value = "/user/userlogin.htm")
	public void userLogin(HttpServletRequest request,ModelMap model,
			HttpServletResponse response,@ModelAttribute("user") Users user) throws IOException {
		if (user.getLoginName() == null || user.getLoginPwd() == null) {
			response.getWriter().print(0);
			return;
		}
		user.setLoginPwd(MD5Util.md5Hex(user.getLoginPwd()));
		List<Users> list = userService.findBySelective(user);
		if (list != null && list.size() > 0) {
			request.getSession().setAttribute("user", list.get(0));
			response.getWriter().print(1);
		}else {//登录失败
			response.getWriter().print(0);
		}
	}
	
	/**
	 * 后台用户注销
	 * @throws IOException 
	 */
	@RequestMapping(value = "/user/userlogout.htm")
	public void UserLogout(HttpServletRequest request,ModelMap model,
			HttpServletResponse response,@ModelAttribute("user") Users user) throws IOException {
		request.getSession().invalidate();
		response.getWriter().print(1);
	}

}
